package com.fly.snailrun.domain.dto;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fly.snailrun.base.constant.JianYinConstant;
import com.fly.snailrun.utils.JianYinUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

@Data
public class DraftMetaInfo {

    @JsonIgnore
    private JSONObject originMetaInfo;

    public DraftMetaInfo() {
        init();
    }

    @JsonProperty("draft_fold_path")
    private String draftFoldPath;

    @JsonProperty("draft_materials")
    private List<DraftMaterials> draftMaterials;


    public void appendMaterialType(List<DraftMaterialsValue> values) {
        DraftMaterials materials = CollUtil.findOne(this.draftMaterials, t -> JianYinConstant.DRAFT_MATERIALS_TYPE.equals(t.getType()));
        List<DraftMaterialsValue> materialsValue = materials.getValue();
        materialsValue.addAll(values);
        materials.setValue(materialsValue);
        originMetaInfo.put(JianYinConstant.DRAFT_MATERIALS, draftMaterials);
        JianYinUtil.write(originMetaInfo.toString());
    }

    public void init() {
        originMetaInfo = JianYinUtil.getDraftMetaInfo();
        JSONArray jsonArray = originMetaInfo.getJSONArray(JianYinConstant.DRAFT_MATERIALS);
        this.draftMaterials = jsonArray.toJavaList(DraftMaterials.class);
    }
}
